export default {
	
	/* @target 解析路由数据和权限数据,设置路由,返回权限
	 * @param menuList 菜单权限数组
	 * @return 权限对象
	 */
	getPermit : function(menuList) {
		//页面映射表
		var pageMap = require('@'+ process.env.PROJECT +'/router/pageMap');
		//页面路由数组
		var routes = [];
		//页面权限对象
		var permit = {};
		
		if(!menuList) {
			return permit;
		}
		//遍历第一层
		menuList.forEach(function(one) {
			//页面类型,添加
			if(one.type==1) {
				addPage(one);
			}
			if(one.type==0 && one.list) {
				//遍历第二层
				 one.list.forEach(function(two) {
				 	//页面类型,添加
				 	if(two.type==1) {
				 		addPage(two);
				 	}
				 	if(two.type==0 && two.list) {
				 		//遍历第三层
				 		two.list.forEach(function(three) {
				 			//页面类型,添加
				 			if(three.type==1) {
				 				addPage(three);
				 			}
				 		})
				 	}
				 })
			}
		})
		
		//如果页面路由不为0,则创建一个模块添加到系统路由中
		if(routes.length!=0) {
			router.addRoutes([{
				path : "",
				name : "主布局页面",
				components : pageMap["home"],
				children : routes
			}]);
		}
		
		function addPage(obj) {
			var page = pageMap[obj.viewId];
			//页面编号为0或者不存在
			if(obj.viewId == 0 || page==null) {
				page = pageMap["none"];
			}
			//外部页面
			if(obj.linkType == 2) {
				//截取外部页面链接的前缀通配和后缀,如  [soccer_url]/xxx/xx
				let url = obj.url;
				obj.url = url.substring(url.indexOf("]") + 1);
				permit[obj.url] = url.substring(url.indexOf("[") + 1, url.indexOf("]"));
				page = pageMap["outside"];
			}
			//不同项目存在不同页面
			if(page[config.project]) {
				page = page[config.project];
			}
			var r = {
				path: obj.url,
				name: obj.name,
				meta : {
					id : obj.menuId
				},
				components : page
			};
			//当前页面是需要全屏显示
			if(obj.linkOpenType == 2) {
				//页面直接加到系统路由对象
				router.addRoutes([r]);
			} else {
				//页面加入到路由数组
				routes.push(r);
			}
			
			var urls = [];
			if(obj.list) {
				obj.list.forEach(function(item){
					urls.push(item.url);
				})	
			}
			
			//页面加入到权限对象
			permit[obj.viewId] = {
				path : obj.url,
				urls : urls
			}
		}
		return permit;
	},
	
	/* @target 获取导航菜单和首页路径
	 * @param menuList 菜单权限数组
	 */
	getNav : function(menuList) {
		
		//导航数据
		var nav = [];
		var firstPath = null;
		
		//遍历第一层
		menuList.forEach(function(one) {
			//剔除type不是0,1和不要求显示的情况
			//!one.showNav || 
			if((one.type!=0 && one.type!=1)) {
				return;
			}
			
			//设置默认图片
			one.icon = (one.icon? one.icon :"default.png" );
			
			//二级菜单数组
			var second = [];
			//如果二级菜单存在,进行遍历
			if(one.type==0 && one.list) {
				one.list.forEach(function(two){
					//剔除...
					//!two.showNav || 
					if((one.type!=0 && one.type!=1)) {
						return;
					}
					//三级菜单数组
					var third = [];
					//如果三级菜单存在,进行遍历
					if(two.type==0 && two.list) {
						var active = true;
						two.list.forEach(function(three) {
							//三级菜单是页面并且允许显示
							// && three.showNav
							if(three.type==1) {
								//将名字中#部分截取(#代表备注,不显示,区分同名菜单)
								let n = three.name;
								let i = n.lastIndexOf("#");
								n = (i ==-1? n : n.substring(0, i));
								//保存三级菜单
								third.push({
									id: three.menuId,
									name : n,
									path : three.url,
									showNav : three.showNav,
									active : active
								})
								//获取首个路径
								getFirstPath(three);
								active = false;
							}
						})
						
					}
					//将名字中#部分截取(#代表备注,不显示,区分同名菜单)
					let n = two.name;
					let i = n.lastIndexOf("#");
					n = (i ==-1? n : n.substring(0, i));
					//保存二级菜单
					second.push({
						id: two.menuId,
						name : n,
						path : two.url,
						type : two.type,
						showNav : two.showNav,
						third : third
					});
					//获取首个路径
					getFirstPath(two);
				});
			}
			
			//将名字中#部分截取(#代表备注,不显示,区分同名菜单)
			let n = one.name;
			let i = n.lastIndexOf("#");
			n = (i ==-1? n : n.substring(0, i));
			//保存一级菜单
			nav.push({
					id: one.menuId,
					name : n,
					path: one.url,
					type : one.type,
					showNav : one.showNav,
					second : second,
					icon : require('@/assets/img/menu/'+one.icon), //要显示的图标
					icon_default : require('@/assets/img/menu/'+one.icon), //默认的图标
					icon_active : require('@/assets/img/menu/active_'+one.icon), //选中的图标
			});
			//获取首个路径
			getFirstPath(one);
		});
		
		function getFirstPath(obj) {
		
			if(obj.type==1 && obj.showNav && !firstPath) {				
				firstPath = obj.url;
			  
			}
		}
			
		return {nav,firstPath};
	},

}